package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.dao.FamilyMemberDao;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.family.FamilyMember;

public class FamilyMemberServlet extends BaseServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String m = request.getParameter("m");
		if ("list".equals(m)) {
			process(request, response, "/page/familyMember/familyMember_list.jsp");
		} else if ("input".equals(m)) {
			process(request, response, "/page/familyMember/familyMember_add.jsp");
		} else if ("add".equals(m)) {
			add(request, response);
			process(request, response, "/page/familyMember/familyMember_list.jsp");
		} else if ("get".equals(m)) {
			process(request, response, "/page/familyMember/familyMember_edit.jsp");
		} else if ("edit".equals(m)) {
			edit(request, response);
			process(request, response, "/page/familyMember/familyMember_list.jsp");
		}else if ("findByIdCard".equals(m)) {
			findByIdCard(request, response);
		} else if ("del".equals(m)) {
			del(request, response);
			process(request, response, "/page/familyMember/familyMember_list.jsp");
		} else {
			error(request, response);
		}
	}
	
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		try {
			// 接收页面传递过来的参数
			String membercode = request.getParameter("first")+request.getParameter("next");
			String cmscode = request.getParameter("cmscode");
			String name = request.getParameter("name");
			String relative = request.getParameter("relative");
			String familycode = request.getParameter("first");
			String idcard = request.getParameter("idcard");
			String sex = request.getParameter("sex");
			String birthday = request.getParameter("birthday");
			String nation = request.getParameter("nation");
			String health = request.getParameter("health");
			String educationallevel = request.getParameter("educationallevel");
			String memberattribute = request.getParameter("memberattribute");
			String farmtype = request.getParameter("farmtype");
			String married = request.getParameter("married");
			String workunit = request.getParameter("workunit");
			String phone = request.getParameter("phone");

			FamilyMember familyMember = new FamilyMember();
			// 调用添加方法
			familyMember.addFamily(membercode, cmscode, name, relative, familycode, idcard, sex, birthday, nation, health, educationallevel, memberattribute, farmtype, married, workunit, phone);;
		} catch (Exception e) {
			error(request, response);
		}
	}

	private void edit(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		try {

			String membercode = request.getParameter("membercode");
			String cmscode = request.getParameter("cmscode");
			String name = request.getParameter("name");
			String relative = request.getParameter("relative");
			String familycode = request.getParameter("familycode");
			String idcard = request.getParameter("idcard");
			String sex = request.getParameter("sex");
			String birthday = request.getParameter("birthday");
			String nation = request.getParameter("nation");
			String health = request.getParameter("health");
			String educationallevel = request.getParameter("educationallevel");
			String memberattribute = request.getParameter("memberattribute");
			String farmtype = request.getParameter("farmtype");
			String married = request.getParameter("married");
			String workunit = request.getParameter("workunit");
			String phone = request.getParameter("phone");
			FamilyMember familyMember = new FamilyMember();
			familyMember.edit(membercode, cmscode, name, relative, familycode, idcard, sex, birthday, nation, health, educationallevel, memberattribute, farmtype, married, workunit, phone);
		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	private void del(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			// 接收行政区域编码+行政区域名称，行政区域名称有可能出现中文乱码
			String membercode = req.getParameter("id");
			FamilyMember familyMember = new FamilyMember();
			familyMember.del(membercode);
		} catch (Exception e) {
			e.printStackTrace();

		}
	}
	
	private void findByIdCard(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		try {
			String idCard = request.getParameter("idcard");
			
			FamilyMemberDao dao=new FamilyMemberDao();
			List<FamilyMember> list=dao.findByIdCard(idCard);
			request.setAttribute("model", list);
		    process(request, response, "/page/apply/apply_find.jsp");
		}catch (Exception e) {
			e.printStackTrace();

		}
	}
}
